﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingsApp.Logic.Import.Model;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.SS.UserModel;

namespace TrainingsApp.Logic.Import.Template {
    public class XLS_ChapterDataReader_1 : ITemplateReader {
        public Dictionary<int, object> ReadAll(int keyId, string location, int templateType) {
            Dictionary<int, object> temp = new Dictionary<int, object>();
            ChapterTemplate_1 root = new ChapterTemplate_1();
            root.Name = "Root";

            ChapterTemplate_1 tempEntity = root;

            string line = string.Empty;
            int bfLevel = 0;
            int level = 0;
            int tempLevel = 0;
            ISheet sheet = null;
            HSSFWorkbook hssfworkbook = null;
            System.Collections.IEnumerator rows = null;
            using (FileStream file = new FileStream(location, FileMode.Open, FileAccess.Read)) {
                hssfworkbook = new HSSFWorkbook(file);
            }
            sheet = hssfworkbook.GetSheetAt(0);
            rows = sheet.GetRowEnumerator();
            int j = 0;
            while (rows.MoveNext()) {

                IRow row = (HSSFRow)rows.Current;
                if (j < 1) { j++; continue; }
                var cell1 = GetString(row.GetCell(0));
                var cell2 = GetString(row.GetCell(1));
                var cell3 = GetString(row.GetCell(2));
                if (j == 1) {
                    root.MetalsId = keyId;
                    root.Name = cell1;
                    root.Level = 0;
                    root.Code = cell3;
                    j++;
                    continue;
                }

                if (string.IsNullOrEmpty(cell2.Trim()) && !string.IsNullOrEmpty(cell1) && !string.IsNullOrEmpty(cell3)) {
                    level = 0;
                }
                else if (!string.IsNullOrEmpty(cell1) && !string.IsNullOrEmpty(cell2) && !string.IsNullOrEmpty(cell3)) {
                    level = 1;
                }
                else if (string.IsNullOrEmpty(cell1) && !string.IsNullOrEmpty(cell2) && !string.IsNullOrEmpty(cell3)) {
                    level = 2;
                }
                else {
                    level = 3;
                }

                tempLevel = level;
                while (tempLevel != bfLevel) {
                    if (tempLevel < bfLevel) {
                        tempEntity = tempEntity.Parent;
                        bfLevel--;
                    }
                    else {
                        tempEntity = tempEntity.Children.LastOrDefault();
                        bfLevel++;
                    }
                }
                if (tempEntity != null) {
                    tempEntity.Children.Add(new ChapterTemplate_1 {
                        Name = (cell1 + " " + cell2).Trim(),
                        Parent = tempEntity,
                        Level = level,
                        Code = cell3
                    });
                }
            }

            //using (FileStream fStream = new FileStream(location, FileMode.Open, FileAccess.Read)) {
            //    using (StreamReader reader = new StreamReader(fStream, Encoding.UTF8)) {
            //        root.Name = reader.ReadLine();
            //        var tempArr = root.Name.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
            //        root.Name = tempArr[1];
            //        root.MetalsId = Convert.ToInt32(tempArr[0]);
            //        while (reader.Peek() != -1) {
            //            line = reader.ReadLine();
            //            if (string.IsNullOrEmpty(line)) continue;

            //            if (!line.StartsWith("\t")) {
            //                level = 0;
            //            }
            //            else if (line.StartsWith("\t\t\t")) {
            //                level = 3;
            //            }
            //            else if (line.StartsWith("\t\t")) {
            //                level = 2;
            //            }
            //            else if (line.StartsWith("\t")) {
            //                level = 1;
            //            }
            //            else {
            //                continue;
            //            }
            //            tempLevel = level;
            //            while (tempLevel != bfLevel) {
            //                if (tempLevel < bfLevel) {
            //                    tempEntity = tempEntity.Parent;
            //                    bfLevel--;
            //                }
            //                else {
            //                    tempEntity = tempEntity.Children.LastOrDefault();
            //                    bfLevel++;
            //                }
            //            }
            //            if (tempEntity != null) {
            //                tempEntity.Children.Add(new ChapterTemplate_1 {
            //                    Name = line.Replace("\t", ""),
            //                    Parent = tempEntity
            //                });
            //            }
            //        }
            //    }
            //}
            temp.Add(1, root);
            return temp;
        }

        private string GetString(ICell cell) {
            if (cell == null) return string.Empty;
            else return cell.ToString().Trim();
        }



    }
}
